package _18_剑指OfferII;

public class _012_剑指OfferII左右两边子数组的和相等 {

    public int pivotIndex(int[] nums) {
        int len = nums.length;
        int[] prefix = new int[len];
        prefix[0] = nums[0];
        for (int i = 1; i < len; i++) {
            prefix[i] += prefix[i - 1] + nums[i];
        }

        for (int i = 0; i < len; i++) {
            int left = prefix[i] - nums[i];
            int right = prefix[len - 1] - left - nums[i];
            if (left == right) {
                return i;
            }
        }
        return -1;
    }

}
